package com.tg.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.demo.view.CruisePrePositionTextItem;
import com.tg.app.R;
import com.tg.app.bean.DevicePresetPoints;
import com.tg.app.util.ObjectBoxUtil;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SelectPrePositionAdapter extends BaseAdapter {
    public static final String TAG = SelectPrePositionAdapter.class.getSimpleName();
    protected final PrePositionAdapter.PrePositionListener mPrePositionListener;
    protected final List<Integer> prePositions = new ArrayList<>();
    protected final Context context;

    public void setData(List<Integer> list){
        prePositions.clear();
        prePositions.addAll(list);
        notifyDataSetChanged();
    }

    public void remove(int position){
        prePositions.remove(position);
        notifyDataSetChanged();
    }

    public String getPrePosition(){
        return StringUtils.joinInteger(prePositions, ",");
    }

    public void setData(String text, String uuid){
        if (!StringUtils.isEmpty(text)){
            prePositions.clear();
            String[] strAttr = text.split(",");
            List<DevicePresetPoints> points = ObjectBoxUtil.getDevicePresetPoints(uuid);

            for (String s : strAttr) {
                int num = Integer.parseInt(s);
                for (DevicePresetPoints devicePresetPoints : points){
                    if (num == devicePresetPoints.num){
                        prePositions.add(num);
                        break;
                    }
                }
            }

            notifyDataSetChanged();
        }
    }

    public boolean addData(int index){
        if (!prePositions.contains(index)){
            prePositions.add(index);
            notifyDataSetChanged();
            return true;
        }
        return false;
    }

    public SelectPrePositionAdapter(Context context, PrePositionAdapter.PrePositionListener listener) {
        this.context = context;
        mPrePositionListener = listener;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_preposition_criuse_item, null);
        }
        int num = prePositions.get(position);
        CruisePrePositionTextItem settingItemTextView = (CruisePrePositionTextItem)convertView;

        settingItemTextView.setTv1(String.valueOf(position+1));
        String name = ResourcesUtil.getString(R.string.pre_position) + num;
        settingItemTextView.setTv2(name);
        settingItemTextView.setTv3(ResourcesUtil.getString(R.string.message_delete_selected));
        settingItemTextView.setColorTv2(ResourcesUtil.getResources().getColor(R.color.message_body_textcolor));
        settingItemTextView.setColorTv3(ResourcesUtil.getResources().getColor(R.color.lock_dell_no_call_color));
        settingItemTextView.getTv3().setOnClickListener(view -> {
            if (mPrePositionListener != null){
                mPrePositionListener.click(position);
            }
        });

        return convertView;
    }


    @Override
    public int getCount() {
        return prePositions.size();
    }

    @Override
    public Object getItem(int i) {
        return prePositions.get(i);
    }

    @Override
    public long getItemId(int i) {
        return prePositions.get(i);
    }
}


